/******************************************************
*
* Copyright 2011 OpenAjax Alliance
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************/

var OAA_CI   = OAA_CI || {};
OAA_CI.lists = OAA_CI.lists || {};

/** ================================================================
*
* lists Module initialization
*
*
* =============================================================== */

OAA_CI.lists_last_index = 0;

/** ================================================================
*
* onLoad
*
* @desc Initializes elements module
*
* @return nothing
*
* =============================================================== */

OAA_CI.lists.onLoad = function(oaa_cache_inspector) {

  OAA_CI.lists_nls_strings = document.getElementById("ID_STRINGBUNDLE_LISTS");
  OAA_CI.lists.onShowListOfListsCommand();

};

/** ================================================================
*
* onShowListOfListsCommand
*
* @desc Evaluates the document and populates the XUL controls with DOMElements using a list
*
* @return nothing
*
* =============================================================== */

OAA_CI.lists.onShowListOfListsCommand = function(e) {

  OAA_CI.evaluateDocument();

  var lists_cache = OAA_CI.evaluation.dom_cache.lists_cache;
  // TODO: define sort method
  // lists_cache.sortDOMElements('document_order', true);

  var list_elements = lists_cache.list_elements;
  var start_time = new Date().getTime();

  var xul_treechildren = document.getElementById('ID_TREECHILDREN_LISTS_LIST');
  OAA_CI.removeChildNodes(xul_treechildren);

  var i; // loop counter
  var le; // loop placeholder
  var max = list_elements.length; // loop control

  for (i = 0; i < max; i++) {
    le = list_elements[i];
    OAA_CI.addTreeItem(xul_treechildren, [le.dom_element.tag_name, le.document_order, le.dom_element.id, le.dom_element.class_name, le.dom_element.xpath], le.cache_id);
  }  // end loop

  OAA_CI.updateXULTime(start_time);

};

/** ================================================================
*
* onShowTreeOfListsCommand
*
* @desc Evaluates the document and popluates the XUL controls with DOMElements using a tree
*
* @return nothing
*
* =============================================================== */

OAA_CI.lists.onShowTreeOfListsCommand = function(e) {

  function addDOMElementChildren(xul_treechildren, list_elements) {

    var i;  // loop counter
    var le; // list_element loop placeholder
    var de; // dom_element loop placeholder
    var max = list_elements.length; // loop control

    for (i = 0; i < max; i++) {

      le = list_elements[i];
      de = le.dom_element;
      var xul_treeitem = OAA_CI.createXULElement('treeitem', []);
      OAA_CI.addTreeRow(xul_treeitem, [de.tag_name, le.document_order, de.id, de.class_name, de.xpath], le.cache_id);

      if (le.child_elements.length) {
        xul_treeitem.setAttribute("container", "true");
        xul_treeitem.setAttribute("open", "true");

        var xul_sub_treechildren = OAA_CI.createXULElement('treechildren', []);
        addDOMElementChildren(xul_sub_treechildren, le.child_elements);
        xul_treeitem.appendChild(xul_sub_treechildren);
      }
      xul_treechildren.appendChild(xul_treeitem);
    } // loop
  } // end function

  OAA_CI.evaluateDocument();
  var child_elements = OAA_CI.evaluation.dom_cache.lists_cache.child_elements;
  var start_time = new Date().getTime();
  var xul_treechildren = document.getElementById('ID_TREECHILDREN_LISTS_TREE');

  OAA_CI.removeChildNodes(xul_treechildren);
  addDOMElementChildren(xul_treechildren, child_elements);
  OAA_CI.updateXULTime(start_time);
};

/** ================================================================
*
* onListElementItemsSelectChanged
*
* @desc Updates properties tabpanel with the information from the
*       currently selected item in the ListsCache
*
* @return nothing
*
* =============================================================== */

OAA_CI.lists.onListElementItemsSelectChanged = function (event) {

  var le;
  var col_id;
  var cache_id;

  var index = event.target.currentIndex;
  var lists_cache = OAA_CI.evaluation.dom_cache.lists_cache;
  var xul_tree = document.getElementById('ID_TREE_LISTS_LIST');

  if (xul_tree) {
    col_id   = xul_tree.columns.getFirstColumn();
    cache_id = xul_tree.view.getCellValue(index, col_id);

    OpenAjax.a11y.console("Lists cache id: " + cache_id );
    le = lists_cache.getListElementByCacheId(cache_id);

    if (le) {
      OAA_CI.showCacheItemProperties(le);
    }
  }

};
